package com.cefa.edu.service;

import com.cefa.edu.dao.UserOrganRoleMapper;
import com.cefa.edu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cc664 on 2018/8/13.
 * @aa userOrganRoleService
 */
@Service
public class UserOrganRoleService {
    @Autowired
    private UserOrganRoleMapper userOrganRoleMapper;

    /**
     * @aa 添加userOrganRole关系数据
     * @aa add by chaijian 2018-08-13

    public Integer saveUserOrganRole(User user,Integer organId,Integer roleType){
        //organId不同 sql不同
        Map<String,Object> paramMap=new HashMap<String,Object>();//封装入库的参数

        switch (roleType){
            case 1:
                break;
            case 2:
                //教育阶段管理者
                paramMap.put("userId",user.getId());
                paramMap.put("schoolId",user.getSchoolId());
                paramMap.put("phaseId",organId);
                paramMap.put("roleType",roleType);
                userOrganRoleMapper.saveUserOrganRoleForPhaseManager(paramMap);
                break;
            case 3:
                //根据organId此时是年级id 获取对应的学段id
                List<Integer> phaseIdList=userOrganRoleMapper.getPhaseIdListByGradeId(organId);
                if(phaseIdList!=null && phaseIdList.size()>0){
                    paramMap.put("userId",user.getId());
                    paramMap.put("schoolId",user.getSchoolId());
                    paramMap.put("phaseId",phaseIdList.get(0));
                    paramMap.put("gradeId",organId);
                    paramMap.put("roleType",roleType);
                }

                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
        }
        return 0;
    }
     */
    /**
     * @aa 根据userId、roleType判断userId指定的用户是否已分配了roleType对应的角色(一个角色对应一条数据)
     * @aa 若判断是否分配了科目教师角色，使用userId、roleType、courseId参数
     * @aa add by chaijian 2018-08-13
     */
    public Integer getCountRecordWithUserIdAndRoleTypeAndCourseId(Integer userId,Integer roleType,Integer courseId){
       if(courseId==null ||courseId==0){
           return userOrganRoleMapper.getCountRecordWithUserIdAndRoleType(userId,roleType);
       }else{
           return userOrganRoleMapper.getCountRecordWithUserIdAndRoleTypeAndCourseId(userId,roleType,courseId);
       }

    }

    /**
     * @aa 根据班级id 判断指定角色的关系记录数
     * @aa add by chaijian 2018-08-13
     */
    public int countUserOrganRoleByClassIdAndRoleType(Integer classId) {
      return   userOrganRoleMapper.countUserOrganRoleByClassIdAndRoleType(classId);
    }

    /**
     * @aa 根据学习阶段id 判断该阶段下是否有班级
     * @aa add by chaijian 2018-08-15
     */
    public int countUserOrganRoleByPhaseId(Integer phaseId){
        return  userOrganRoleMapper.countUserOrganRoleByPhaseId(phaseId);
    }
    public int getCountRecordWithUserIdAndRoleType(Integer userId,Integer roleType){
        return userOrganRoleMapper.getCountRecordWithUserIdAndRoleType(userId,roleType);
    }
    public int getUserOrganRole(String UserName,String IdCode,Integer userId,Integer roleType,Integer levelId,Integer courseId){
        return userOrganRoleMapper.getUserOrganRole(UserName,IdCode,userId,roleType,levelId,courseId);
    }

    /**
     * @aa 根据用户id 获取指定学校下 该用户所属角色编号集合
     * @aa add by chaijian 2018-08-30
     */
    public List<Integer> getRoleTypeByUserIdAndSchoolId(Integer userId, Integer schoolId) {
        return userOrganRoleMapper.getRoleTypeByUserIdAndSchoolId(userId,schoolId);
    }

    /**
     * @aa 判断指定科目有无被老师引用
     * @aa add by chaijian 2018-09-05
     */
    public int getCountOfCourseFromUserOrganRoleWithCourseId(Integer courseId) {
        return  userOrganRoleMapper.getCountOfCourseFromUserOrganRoleWithCourseId(courseId);
    }

    public List<Map<String,Object>> getUserOrganRoleByUserId(Integer userId) {
        return userOrganRoleMapper.getUserOrganRoleByUserId(userId);
    }
}
